package cn.mst.vrs.service.ordertype;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.mst.vrs.controller.admin.ordertype.vo.OrderTypePageReqVO;
import cn.mst.vrs.controller.admin.ordertype.vo.OrderTypeSaveReqVO;
import cn.mst.vrs.dal.dataobject.ordertype.OrderTypeDO;
import cn.mst.vrs.dal.mysql.ordertype.OrderTypeMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.mst.vrs.enums.ErrorCodeConstants.ORDER_TYPE_NOT_EXISTS;

/**
 * 订单类型 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class OrderTypeServiceImpl implements OrderTypeService {

    @Resource
    private OrderTypeMapper orderTypeMapper;

    @Override
    public Long createOrderType(OrderTypeSaveReqVO createReqVO) {
        // 插入
        OrderTypeDO orderType = BeanUtils.toBean(createReqVO, OrderTypeDO.class);
        orderTypeMapper.insert(orderType);
        // 返回
        return orderType.getId();
    }

    @Override
    public void updateOrderType(OrderTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderTypeExists(updateReqVO.getId());
        // 更新
        OrderTypeDO updateObj = BeanUtils.toBean(updateReqVO, OrderTypeDO.class);
        orderTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderType(Long id) {
        // 校验存在
        validateOrderTypeExists(id);
        // 删除
        orderTypeMapper.deleteById(id);
    }

    private void validateOrderTypeExists(Long id) {
        if (orderTypeMapper.selectById(id) == null) {
            throw exception(ORDER_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public OrderTypeDO getOrderType(Long id) {
        return orderTypeMapper.selectById(id);
    }

    @Override
    public PageResult<OrderTypeDO> getOrderTypePage(OrderTypePageReqVO pageReqVO) {
        return orderTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<OrderTypeDO> listDict() {
        return orderTypeMapper.selectDictList();
    }

}